package com.enpronomics.pcall;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.net.Uri;
import android.telephony.SmsManager;
import android.widget.Toast;

public class ServiceManager {
	public final static String INVITE_SMS_BODY = "Check this application (codemate) http://codemate.enpronomics.com/codemate.jad";

	public final static String INVITE_BROWSER_FB = "http://www.facebook.com/sharer/sharer.php?u=codemate.enpronomics.com";
	public final static String INVITE_BROWSER_TW = "http://mobile.twitter.com/home?status=Check this application (codemate) http://codemate.enpronomics.com. You can download it from http://codemate.enpronomics.com/codemate.jad";

	public static final String KEY_PANIC = "panic";
	public static final String KEY_NAME = "Add Name";
	public static final String KEY_CONTACT1 = "contact1";
	public static final String KEY_CONTACT2 = "contact2";
	public static final String KEY_CONTACT3 = "contact3";
	public static final String KEY_CONTACT4 = "contact4";
	public static final String KEY_CONTACT5 = "contact5";
	public static final String KEY_LOCATION_AUTO_REPLAY = "locationAutoReplay";
	public static final String KEY_AUTO_START = "autoStart";
	public static final String KEY_SOS_REPETITION = "SOSRepetion";
	public static final String KEY_ACTION_INTERVAL = "actionInterval";
	public static final String KEY_PRIVACY_STATEMENT = "PrivacyStatement";

	public static final String KEY_PANIC_SMS_NUMBER = "panicSMSNumber";
	public static final String KEY_PANIC_CALL_NUMBER = "panicCallNumber";
	public static final String KEY_POSITION_REPORT_NUMBER = "positionReportNumber";
	public static final String KEY_AUTO_ASSISTANCE = "autoAssistance";
	public static final String KEY_AUTO_ASSISTANCE_NUMBER = "autoAssistanceNumber";
	public static final String KEY_REMOTE_REQUEST = "remoteRequest";

	public static final String PANIC_MESSAGE = "My location";
	// public static final String RED_ALERT_MESSAGE =
	// "Need Your Help, Here I am...";
	// public static final String GREEN_ALERT_MESSAGE =
	// "I am Fine, Here I am...";
	public static final String REMOTE_REQUEST = "remote request";
	// public static final String GOT_LOCATION_MESSAGE = "Here I am";

	public static final String MESSAGE_TYPE = "Panic";
	public static final String PANIC_INITIATED = "panicInitiated";

	public static boolean alarm = false;

	public static String getLocationStr(Location location) {
		double latitudeD = location.getLatitude();
		double longitudeD = location.getLongitude();
		BigDecimal latitude = new BigDecimal(latitudeD);
		BigDecimal longitude = new BigDecimal(longitudeD);
		return latitude.setScale(6, RoundingMode.HALF_UP) + ";"
				+ longitude.setScale(6, RoundingMode.HALF_UP);
	}

	public static String getStringSetting(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		return settings.getString(key, "None");
	}

	public static int getIntSetting(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		return settings.getInt(key, -1);
	}

	public static void setIntSetting(Context context, String key, int value) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static void setStringSetting(Context context, String key,
			String value) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static void setStringSettingIfEmpty(Context context, String key,
			String value) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		if (settings.getString(key, "None").equals("None")) {
			Editor editor = settings.edit();
			editor.putString(key, value);
			editor.commit();
		}
	}

	public static void setIntSettingIfEmpty(Context context, String key,
			int value) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		if (settings.getInt(key, -1) == -1) {
			Editor editor = settings.edit();
			editor.putInt(key, value);
			editor.commit();
		}
	}

	public static boolean getBoolSetting(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		return settings.getBoolean(key, false);
	}

	public static void setBoolSetting(Context context, String key, boolean value) {
		SharedPreferences settings = context.getSharedPreferences(
				"pCallSettings", Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static String getCurrentDate() {
		return new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date(System
				.currentTimeMillis()));
		// return
		// DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT).format(new
		// Date(System.currentTimeMillis()));
	}

	public static void openBrowser(Activity activity, String url) {
		Uri uri = Uri.parse(url);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		activity.startActivity(intent);
	}

	public static void call(Context context, String phoneNumber) {
		Intent launch = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phoneNumber));
		launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(launch);
	}

	public static void sendSMS(Context context, String phoneNumber,
			String message) {
		 PendingIntent pi = null;
		 if (context != null){
			 Intent intent=new Intent(context, SMS.class);
			 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			 pi = PendingIntent.getActivity(context, 0, intent, 0);
		 }
		 SmsManager sms = SmsManager.getDefault();
		 sms.sendTextMessage(phoneNumber, null, message, pi, null);
		try{Toast.makeText(context, "SMS Sent to:" + phoneNumber,Toast.LENGTH_LONG).show();}catch (Exception e) {}
	}

//	public static void sendSMS(final Activity activity,
//			final String phoneNumber, final String message, final int timesNo,
//			final int delayInMins) {
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				for (int i = 0; i < timesNo; i++) {
//					// PendingIntent pi = PendingIntent.getActivity(activity, 0,
//					// new Intent(activity, SMS.class), 0);
//					// SmsManager sms = SmsManager.getDefault();
//					// sms.sendTextMessage(phoneNumber, null, message, pi,
//					// null);
//					Toast.makeText(activity, "SMS Sent to:" + phoneNumber,
//							Toast.LENGTH_LONG).show();
//					try {
//						Thread.sleep(delayInMins * 1000 * 60);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			}
//		}).start();
//	}

	public class SMS {
	}
}
